// pages/talent_simulator/index/index.ts
type Talent = {
  id: number;
  name: string;
  description: string;
  icon: string;
  maxPoints: number;
  currentPoints: number;
  requires?: number[];
  position: {
    row: number;
    column: number;
    requiresPositions?: { row: number; column: number }[];
  };
  unlockRequirement?: {
    row: number;
    requiredPoints: number;
  };
};

type TalentTree = {
  name: string;
  icon: string;
  talents: Talent[];
};


Page({

  data: {
    classes: [
      {
        name: '战士',
        icon: '/images/warrior.png',
        trees: [
          {
            name: '武器',
            icon: '/images/arms.png',
            talents: this.generateWarriorArmsTalents(),
          },
        ],
      },
    ] as Class[],
    selectedClassIndex: 0,
    selectedTreeIndex: 0,
    totalPoints: 51,
    usedPoints: 0,
    connections: [] as Connection[],
  },

  onLoad() {
    this.loadSavedTalents();
    this.updateConnections();
  },
  saveTalents() {
    wx.setStorageSync('talentBuild', this.data.classes);
    wx.showToast({ title: '方案已保存' });
  },

  loadSavedTalents() {
    const saved = wx.getStorageSync('talentBuild');
    if (saved) this.setData({ classes: saved });
  },
  generateWarriorArmsTalents(): Talent[] {
    return [
      {
        id: 1,
        name: '强化英勇打击',
        description: '英勇打击怒气消耗减少3点',
        icon: '/icons/heroic-strike.png',
        maxPoints: 3,
        currentPoints: 0,
        position: { row: 1, column: 1, requiresPositions: [] },
      },
      {
        id: 2,
        name: '偏斜',
        description: '招架几率提高5%',
        icon: '/icons/parry.png',
        maxPoints: 5,
        currentPoints: 0,
        position: { row: 1, column: 2, requiresPositions: [] },
      },
      {
        id: 3,
        name: '强化撕裂',
        description: '撕裂伤害提高30%',
        icon: '/icons/rend.png',
        maxPoints: 3,
        currentPoints: 0,
        requires: [1],
        position: { 
          row: 2, 
          column: 1,
          requiresPositions: [{ row: 1, column: 1 }]
        },
        unlockRequirement: { row: 1, requiredPoints: 5 }
      },
    ];
  },

  updateConnections() {
    const connections: Connection[] = [];
    const currentTalents = this.getCurrentTalents();

    currentTalents.forEach(talent => {
      talent.position.requiresPositions?.forEach(startPos => {
        const startTalent = currentTalents.find(t => 
          t.position.row === startPos.row && 
          t.position.column === startPos.column
        );
        
        if (startTalent) {
          connections.push({
            from: startPos,
            to: { row: talent.position.row, column: talent.position.column },
            active: startTalent.currentPoints > 0 && talent.currentPoints > 0
          });
        }
      });
    });

    this.setData({ connections });
  },

  // 其他方法保持不变，增加连线更新
  tapTalent(e:any) {
    // ...原有逻辑
    this.updateConnections();
  },
});

interface Connection {
  from: { row: number; column: number };
  to: { row: number; column: number };
  active: boolean;
}